package com.whzykj.resource;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import net.sf.json.JSONObject;

import com.jfinal.plugin.activerecord.Record;
import com.whzykj.common.AmountUtils;
import com.whzykj.common.CommonUtils;
import com.whzykj.common.JsonUtil;
import com.whzykj.common.ReturnEnum;
import com.whzykj.common.WxConst;
import com.whzykj.common.alipay.AlipayUtil;
import com.whzykj.common.wx.PayUtil;
import com.whzykj.model.Account;
import com.whzykj.model.OrderInfo;
import com.whzykj.model.Userinfo;
import com.whzykj.model.VirtualOrder;

public class AliPayResource {

	/****
	 * 支付宝订单支付接口
	 * @param messageObj
	 * @param result
	 * http://localhost:8080/bcy/mobile/Interface?content={namespace:'wxpay',type:'pay',order_id:'APP20160724062217310',balance_pay:''}
	 * 
	 */
	public void pay(JSONObject messageObj, JSONObject result){
		
		String order_id = messageObj.getString("order_id");// 订单号
		String balance_pay = messageObj.getString("balance_pay");//余额支付金额，如果没有使用余额支付传0；
		
		String sql=" SELECT * FROM orderinfo r where r.STATUS='no_pay' and r.order_id='"+order_id+"' ";
		OrderInfo order = OrderInfo.me.findFirst(sql);
		if(order == null)
		{
			JsonUtil.put(result, ReturnEnum.NO_PAY_ORDER);
			return;
		}else{
			
			//用户ID
			String cust_id = String.valueOf(order.getInt("cust_id"));
			String user_id = String.valueOf(order.getInt("user_id"));
			
			//订单金额
			BigDecimal order_amounts = order.getBigDecimal("pay_amounts");
			
			//客户ID
			String acctsql=" SELECT * FROM account WHERE cust_id=? ";
			Account acct = Account.dao.findFirst(acctsql, cust_id);
			
			//余额支付金额
			BigDecimal balancepay =null;
			if(balance_pay == null || "".equals(balance_pay) || "0".equals(balance_pay))
			{
				balancepay = new BigDecimal("0");
			}else{
				balancepay = acct.getBigDecimal("amount");
			}
			//实际支付金额
			BigDecimal balance = order_amounts.subtract(balancepay);//减去支付金额后剩余余额
			//实付金额
			BigDecimal pay_amounts = AmountUtils.formatComma2BigDecimal(balance.toString());
			
			String links="";
			try {
				links = AlipayUtil.unifiedorderfororder(order_id, pay_amounts,balancepay,user_id);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.put("links", links);
			JsonUtil.put(result, ReturnEnum.BALANCE_PAY_SUCCESS);
			
			
		}
	}
	
	
	/****
	 * 支付宝余额充值接口
	 * @param messageObj
	 * @param result
	 * http://localhost:8080/bcy/mobile/Interface?content={namespace:'aliapy',type:'balancepay',balance_pay:'',user_id:''}
	 * 
	 */
	public void balancepay(JSONObject messageObj, JSONObject result){
		// 用户ID
		String user_id=messageObj.getString("user_id");
		String balance_pay = messageObj.getString("balance_pay");//充值金额
		
		Record userRd = Userinfo.dao.userInfoByuserid(user_id);
		//客户ID
		String cust_id = String.valueOf(userRd.getInt("cust_id"));
		
		String acctsql=" SELECT * FROM account WHERE cust_id=? ";
		Account acct = Account.dao.findFirst(acctsql, cust_id);
		//账户ID
		String acct_id = String.valueOf(acct.getInt("acct_id"));
		
		//流水号
		String tid = CommonUtils.generateVirtualOrder();
		
		VirtualOrder vir = new VirtualOrder();
		vir.set("tid", tid);
		vir.set("order_type", "CZ");//充值
		vir.set("cust_id", cust_id);
		vir.set("acct_id", acct_id);
		vir.set("user_id", user_id);
		vir.set("pay_amounts", balance_pay);
		vir.set("pay_method", "ZFB");
		vir.set("status", "NOPAY");
		vir.set("create_date", new Date());
		vir.save();
		
		//实付金额
		BigDecimal pay_amounts = AmountUtils.formatComma2BigDecimal(balance_pay);
		
		String links="";
		try {
			links = AlipayUtil.unifiedorder(tid, pay_amounts);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result.put("links", links);
		JsonUtil.put(result, ReturnEnum.BALANCE_PAY_SUCCESS);
	}
	
}
